package demo1;

import java.util.Date;

public class MyLinkedList {
    public Node head;
    public Node last;
    public void creatList() {
        Node node1 = new Node(23);
        Node node2 = new Node(23);
        Node node3 = new Node(232);
        Node node4 = new Node(23);
        node1.prev = null;
        node1.next = node2;
        node2.prev = node1;
        node2.next = node3;
        node3.prev = node2;
        node3.next = node4;
        node4.prev = node3;
        node4.next = null;
        this.head = node1;
        this.last = node4;
    }
    public void display() {
        Node cur = this.head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void displayNode(Node node) {
        Node cur = node;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }
    //逆序输出
    public void reverseOrderDisplay() {
        Node cur = this.last;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.prev;
        }
        System.out.println();
    }

    //头插
    public void addFirst(int data) {
        Node cur = this.head;
        Node newNode = new Node(data);
        if (isEmpty()) {
            this.head = newNode;
            this.last = newNode;
            return;
        }
        newNode.next = cur;
        cur.prev = newNode;
        this.head = newNode;
    }

    //尾插
    public void addLast(int data) {
        Node cur = this.last;
        Node newNode = new Node(data);
        if (isEmpty()) {
            this.head = newNode;
            this.last = newNode;
            return;
        }

        /*while (cur.next != null) {
            cur = cur.next;
        }
        newNode.prev = cur;
        cur.next = newNode;*/
        newNode.prev = cur;
        cur.next = newNode;
        this.last = newNode;
    }

    //任意位置插入,第一个数据节点为0号 下标
    public boolean addIndex(int index, int data) {
        if (index < 0 || index > size() ) {
            System.out.println("插入位置异常!");
            return false;
        }
        if (index == 0) {
            addFirst(data);
            return true;
        }
        if (index == size()) {
            addLast(data);
            return true;
        }

        Node cur = this.head;
        Node newNode = new Node(data);

        while (--index > 0) {
            cur = cur.next;
        }
        newNode.prev = cur;
        newNode.next = cur.next;
        cur.next = newNode;
        cur = newNode.next;
        cur.prev = newNode;
        return true;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        if (isEmpty()) {
            System.out.println("空链表");
            return false;
        }
        Node cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (isEmpty()) {
            return;
        }
        Node cur = this.head;

        /*if (cur.val == key) {
            this.head = this.head.next;
            this.head.prev = null;
            return;
        }

        while (cur.next != null) {
            if (cur.next.val == key) {
                Node del = cur.next;
                cur.next = del.next;
                if (del.next != null) {
                    del = del.next;
                    del.prev = cur;
                }
                return;
            }
            cur = cur.next;
        }*/

        while (cur != null) {
            if(cur.val == key) {
                if (cur == this.head) {
                    this.head = this.head.next;
                    if (this.head != null) {
                        this.head.prev = null;
                    } else {
                        this.last = null;
                    }

                    return;
                } else if (cur.next == null) {
                    cur.prev.next = cur.next;
                    this.last = cur.prev;
                    return;
                }  else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                    return;
                }
            }
            cur = cur.next;
        }


    }

    //删除所有key关键字
    public void removeAllkey(int key) {
        if (isEmpty()) {
            return;
        }
        Node cur = this.head;
        /*Node curNext = this.head.next;
        Node cur = this.head;
        Node del = null;
        while (curNext != null) {
            if (curNext.val == key) {
                cur.next = curNext.next;
                del = curNext.next;
                if(del != null) {
                    del.prev = cur;
                }
            } else {
                cur = curNext;
            }
            curNext = curNext.next;
        }

        if (this.head.val == key) {
            cur.prev = null;
            this.head = cur;
        }*/

        while (cur != null) {
            if(cur.val == key) {
                if (cur == this.head) {
                    this.head = this.head.next;
                    if (this.head != null) {
                        this.head.prev = null;
                    } else {
                        this.last = null;
                    }

                } else if (cur.next == null) {
                    cur.prev.next = cur.next;
                    this.last = cur.prev;
                }  else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
            }
            cur = cur.next;
        }

    }


    public boolean isEmpty() {
        if (this.head == null) {
            return true;
        }
        return false;
    }

    public int size() {
        Node cur = this.head;
        int count = 0;
        while (cur != null) {
            cur =  cur.next;
            count++;
        }
        return count;
    }

    public void clear() {
        Node del = this.head;
        while (this.head != null) {
            del = this.head.next;
            this.head.next = null;
            this.head.prev = null;
            this.head = del;
        }
        this.last = null;
    }
}
